package com.sta.oldman.utils;

import java.security.SecureRandom;
import java.util.Date;

/**
 * 6位验证码生成工具类
 * 支持纯数字验证码、数字+字母混合验证码
 */
public class VerificationCodeUtils {

    // 数字字符集
    private static final String NUMBERS = "0123456789";
    // 字母字符集（排除易混淆的i、I、l、L、o、O、0、1）
    private static final String LETTERS = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
    // 数字+字母混合字符集
    private static final String NUMBERS_AND_LETTERS = NUMBERS + LETTERS;

    // 安全随机数生成器（比Random更安全，适合生成验证码）
    private static final SecureRandom SECURE_RANDOM = new SecureRandom();

    /**
     * 生成6位纯数字验证码
     * @return 6位数字字符串
     */
    public static String generateNumericCode() {
        StringBuilder codeBuilder = new StringBuilder(6);
        for (int i = 0; i < 6; i++) {
            // 从数字字符集中随机选取一个字符
            int index = SECURE_RANDOM.nextInt(NUMBERS.length());
            codeBuilder.append(NUMBERS.charAt(index));
        }
        return codeBuilder.toString();
    }


    /**
     * 验证验证码是否有效（忽略字母大小写）
     * @param inputCode 用户输入的验证码
     * @param correctCode 正确的验证码
     * @return 验证通过返回true，否则返回false
     */
    public static boolean validateCode(String inputCode, String correctCode) {
        // 校验参数合法性
        if (inputCode == null || correctCode == null) {
            return false;
        }
        // 去除前后空格并忽略大小写比较（仅对混合验证码有效，数字验证码不影响）
        return inputCode.trim().equalsIgnoreCase(correctCode.trim());
    }

    /**
     * 生成带过期时间的验证码对象（可用于需要时效性的场景）
     * @return 包含验证码和过期时间的对象
     */
    public static CodeWithExpiration generateCodeWithExpiration() {
        String code =  generateNumericCode();
        // 默认5分钟后过期（单位：毫秒）
        long expirationTime = System.currentTimeMillis() + 5 ;
        return new CodeWithExpiration(code, expirationTime);
    }

    /**
     * 内部类：包含验证码和过期时间的对象
     */
    public static class CodeWithExpiration {
        private final String code;
        private final long expirationTime; // 过期时间戳（毫秒）

        public CodeWithExpiration(String code, long expirationTime) {
            this.code = code;
            this.expirationTime = expirationTime;
        }

        public String getCode() {
            return code;
        }

        public long getExpirationTime() {
            return expirationTime;
        }

        /**
         * 判断验证码是否已过期
         * @return 已过期返回true，否则返回false
         */
        public boolean isExpired() {
            return System.currentTimeMillis() > expirationTime;
        }
    }
    // 在QQEmailUtils中添加生成验证码HTML的方法
    public static String createVerificationCodeHtml(String code) {
        return "<!DOCTYPE html>"
                + "<html lang=\"zh-CN\">"
                + "<head>"
                + "<meta charset=\"UTF-8\">"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
                + "<title>验证码验证</title>"
                + "<style>"
                + "    body { margin: 0; padding: 0; background-color: #f7f9fc; font-family: 'Microsoft YaHei', sans-serif; }"
                + "    .email-container { max-width: 600px; margin: 0 auto; background-color: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 20px rgba(0,0,0,0.08); }"
                + "    .email-header { background: linear-gradient(135deg, #4a90e2 0%, #5c6bc0 100%); padding: 30px 20px; text-align: center; }"
                + "    .header-title { color: #ffffff; margin: 0; font-size: 24px; font-weight: 600; }"
                + "    .email-body { padding: 30px 40px; }"
                + "    .greeting { color: #333333; font-size: 18px; margin-bottom: 20px; }"
                + "    .content-text { color: #666666; line-height: 1.8; font-size: 16px; margin-bottom: 30px; }"
                + "    .code-container { background-color: #f8f9fa; border-radius: 8px; padding: 25px 20px; text-align: center; margin-bottom: 30px; }"
                + "    .verification-code { font-size: 32px; font-weight: bold; color: #4a90e2; letter-spacing: 8px; margin: 0; padding: 10px 0; }"
                + "    .expire-info { color: #999999; font-size: 14px; margin-top: 15px; }"
                + "    .note-text { color: #ff5252; font-size: 14px; line-height: 1.6; padding: 15px; background-color: #fff8f8; border-left: 4px solid #ff5252; border-radius: 4px; margin-bottom: 30px; }"
                + "    .email-footer { padding: 20px 40px; background-color: #f8f9fa; border-top: 1px solid #eee; }"
                + "    .footer-text { color: #999999; font-size: 12px; text-align: center; margin: 0; line-height: 1.6; }"
                + "    @media (max-width: 600px) {"
                + "        .email-container { border-radius: 0; }"
                + "        .email-body { padding: 20px 15px; }"
                + "        .verification-code { font-size: 26px; letter-spacing: 5px; }"
                + "    }"
                + "</style>"
                + "</head>"
                + "<body>"
                + "    <div class=\"email-container\">"
                + "        <div class=\"email-header\">"
                + "            <h1 class=\"header-title\">安全验证</h1>"
                + "        </div>"
                + "        <div class=\"email-body\">"
                + "            <p class=\"greeting\">您好！</p>"
                + "            <p class=\"content-text\">"
                + "                您正在进行身份验证操作，以下是您的验证码。请在验证页面输入此验证码，完成操作。"
                + "            </p>"
                + "            <div class=\"code-container\">"
                + "                <p class=\"verification-code\">" + code + "</p>"
                + "                <p class=\"expire-info\">验证码 " + 5 + " 分钟内有效，请勿向他人泄露</p>"
                + "            </div>"
                + "            <p class=\"note-text\">"
                + "                注意：如非本人操作，请忽略此邮件。切勿将验证码透露给他人，以保障您的账号安全。"
                + "            </p>"
                + "        </div>"
                + "        <div class=\"email-footer\">"
                + "            <p class=\"footer-text\">"
                + "                此邮件为系统自动发送，请勿直接回复<br>"
                + "                © 2025 安全验证服务 版权所有"
                + "            </p>"
                + "        </div>"
                + "    </div>"
                + "</body>"
                + "</html>";
    }
    public static String createAlertHtml( String alertMessage,String time) {

        return "<!DOCTYPE html>\n" +
                "<html lang=\"zh-CN\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>系统警报</title>\n" +
                "    <style>\n" +
                "        * {\n" +
                "            margin: 0;\n" +
                "            padding: 0;\n" +
                "            box-sizing: border-box;\n" +
                "            font-family: 'Arial', sans-serif;\n" +
                "        }\n" +
                "        \n" +
                "        body {\n" +
                "            background-color: #f8f9fa;\n" +
                "            display: flex;\n" +
                "            justify-content: center;\n" +
                "            align-items: center;\n" +
                "            min-height: 100vh;\n" +
                "            padding: 20px;\n" +
                "        }\n" +
                "        \n" +
                "        .alert-container {\n" +
                "            width: 100%;\n" +
                "            max-width: 600px;\n" +
                "            background-color: white;\n" +
                "            border-radius: 10px;\n" +
                "            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);\n" +
                "            overflow: hidden;\n" +
                "        }\n" +
                "        \n" +
                "        .alert-header {\n" +
                "            background-color: #dc3545;\n" +
                "            color: white;\n" +
                "            padding: 20px;\n" +
                "            text-align: center;\n" +
                "            position: relative;\n" +
                "        }\n" +
                "        \n" +
                "        .alert-header h1 {\n" +
                "            font-size: 24px;\n" +
                "            margin-bottom: 5px;\n" +
                "        }\n" +
                "        \n" +
                "        .alert-header .icon {\n" +
                "            font-size: 40px;\n" +
                "            margin-bottom: 15px;\n" +
                "        }\n" +
                "        \n" +
                "        .alert-content {\n" +
                "            padding: 25px;\n" +
                "        }\n" +
                "        \n" +
                "        .alert-message {\n" +
                "            background-color: #fff3cd;\n" +
                "            border-left: 4px solid #ffc107;\n" +
                "            padding: 15px;\n" +
                "            margin-bottom: 20px;\n" +
                "            border-radius: 0 4px 4px 0;\n" +
                "        }\n" +
                "        \n" +
                "        .alert-details {\n" +
                "            margin-bottom: 25px;\n" +
                "        }\n" +
                "        \n" +
                "        .detail-item {\n" +
                "            display: flex;\n" +
                "            margin-bottom: 10px;\n" +
                "        }\n" +
                "        \n" +
                "        .detail-item strong {\n" +
                "            width: 120px;\n" +
                "            color: #6c757d;\n" +
                "        }\n" +
                "        \n" +
                "        .alert-actions {\n" +
                "            display: flex;\n" +
                "            justify-content: center;\n" +
                "            gap: 15px;\n" +
                "            margin-top: 30px;\n" +
                "        }\n" +
                "        \n" +
                "        .btn {\n" +
                "            padding: 10px 20px;\n" +
                "            border: none;\n" +
                "            border-radius: 5px;\n" +
                "            cursor: pointer;\n" +
                "            font-weight: bold;\n" +
                "            transition: all 0.3s;\n" +
                "        }\n" +
                "        \n" +
                "        .btn-primary {\n" +
                "            background-color: #007bff;\n" +
                "            color: white;\n" +
                "        }\n" +
                "        \n" +
                "        .btn-primary:hover {\n" +
                "            background-color: #0069d9;\n" +
                "        }\n" +
                "        \n" +
                "        .btn-secondary {\n" +
                "            background-color: #6c757d;\n" +
                "            color: white;\n" +
                "        }\n" +
                "        \n" +
                "        .btn-secondary:hover {\n" +
                "            background-color: #5a6268;\n" +
                "        }\n" +
                "        \n" +
                "        .btn-right {\n" +
                "          background-color: rgb(3, 207, 3);\n" +
                "          display: none;\n" +
                "        }\n" +
                "        .btn-right:hover {\n" +
                "            background-color: rgb(3, 168, 3);\n" +
                "        }\n" +
                "        .timestamp {\n" +
                "            text-align: right;\n" +
                "            color: #6c757d;\n" +
                "            font-size: 14px;\n" +
                "            margin-top: 20px;\n" +
                "        }\n" +
                "        \n" +
                "        /* 动画效果 */\n" +
                "        @keyframes pulse {\n" +
                "            0% { transform: scale(1); }\n" +
                "            50% { transform: scale(1.05); }\n" +
                "            100% { transform: scale(1); }\n" +
                "        }\n" +
                "        \n" +
                "        .pulse {\n" +
                "            animation: pulse 2s infinite;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"alert-container\">\n" +
                "        <div class=\"alert-header pulse\">\n" +
                "            <div class=\"icon\">⚠\uFE0F</div>\n" +
                "            <h1>紧急警报</h1>\n" +
                "        </div>\n" +
                "        \n" +
                "        <div class=\"alert-content\">\n" +
                "            <div class=\"alert-details\">\n" +
                "                <h3>警报详情</h3>\n" +
                "                <div class=\"detail-item\">\n" +
                "                    <strong>警报类型：</strong>\n" +
                "                    <!-- TODO -->\n" +
                "                    <span>"+alertMessage+"</span>\n" +
                "                </div>\n" +
                "                <div class=\"detail-item\">\n" +
                "                    <strong>严重程度：</strong>\n" +
                "                    <span style=\"color: #dc3545;\">严重</span>\n" +
                "                </div>\n" +
                "                <div class=\"detail-item\">\n" +
                "                    <strong>来源IP：</strong>\n" +
                "                    <span>192.168.1.103</span>\n" +
                "                </div>\n" +
                "                <div class=\"detail-item\">\n" +
                "                    <strong>发生时间：</strong>\n" +
                "                    <span >"+time+"</span>\n" +
                "                </div>\n" +
                "            </div>\n" +
                "\n" +
                "        </div>\n" +
                "    </div>\n" +
                "\n" +
                "\n" +
                "</body>\n" +
                "</html>";
    }
}